/** 安卓SDK */
const sendHandleMessage = (str) => {
    try {
        let environment = AndroidSDK.environment;
        if (environment && 'dotnet' === environment) {
            chrome.webview.hostObjects.bridge.handleMessage(str);
            return false;
        }
        parent.window.android.handleMessage(str);
    } catch (e) {
        //   如果调用将会死循环
        // AndroidSDK.writeLog("sendHandleMessage-->error" + JSON.stringify(e.stack));
        console.log('*******')
        // AndroidSDK.writeLog("sendHandleMessage-->error");
    }
};
/**
 * 安卓提供的方法 统一调用入口
 */
const AndroidSDK = {
    environment: '',
    /**
     * 键盘
     */
    showWebKeyboard: () => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "showWebKeyboard"
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog("showWebKeyboard-->" + JSON.stringify(e?.stack));
        }
    },
    /**
     * 写日志
     * @param message
     */
    writeLog: (message) => {
        try {
            let str = JSON.stringify({
                action: "CommonAction",
                callback: "",
                bizFun: "setHtmlLog",
                data: message
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("writeLog-->" + message);
        }
    },
    /**
     * 解决页面白屏问题，如果页面白屏了，安卓端不会收到前端的pong,安卓则认为前端加载页面失败了，会reload,最多加载5次，每次间隔3S
     */
    androidPong: () => {
        try {
            let str = JSON.stringify({
                action: "CommonAction",
                callback: "",
                bizFun: "pong"
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("加载异常");
        }
    },
    /**
     * 获取终端信息
     */
    getDeviceInfo: () => {
        try {
            let str = JSON.stringify({
                action: "CommonAction",
                callback: "getDeviceInfoCallBack",
                bizFun: "getDeviceInfo"
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("getDeviceInfo-->" + "获取终端信息失败");
        }
    },

    /**
     * 获取医保数据
     */
    getMedicalInsuranceData: (message) => {
        try {
            let str = JSON.stringify({
                action: "CommonAction",
                callback: "",
                bizFun: "getMedicalInsuranceData",
                data: message
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("getMedicalInsuranceData 失败");
        }
    },


    /**
     * 获取监听UUID
     */
    getTerminalUuid: (data) => {
        try {
            let str = JSON.stringify({
                action: "CommonAction",
                callback: "getTerminalUuidCallBack",
                bizFun: "getTerminalUuid",
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("获取终端信息失败");
        }
    },

    /**
     * 播放视频文件
     * @param data
     * @param biz
     * @param callback
     */
    playVideoMedias: (data = {}, biz = "playVideoWithSyn", callback = "") => {
        try {
            let str = JSON.stringify({
                action: "PlayVideoAction",
                callback: callback,
                bizFun: biz,
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog("playVideoMedias-->" + JSON.stringify(e?.stack));
        }
    },

    /***
     *  @desc 报到机-报到操作
     *  @param {Object} data
     * **/
    queueReport: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "report",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
            //registerId, signType, worker
        } catch (e) {
            // AndroidSDK.writeLog("queueReport-->" + JSON.stringify(e?.stack));
        }
    },

    /***
     *  @desc 报到机-报到成功后回传给安卓原始数据
     *  @param {Object} data
     * **/
    queueReportResult: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "batchSignResult",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
            //registerId, signType, worker
        } catch (e) {
            // AndroidSDK.writeLog("queueReportResult -->" + JSON.stringify(e?.stack));
        }
    },
    /***
     *  @desc 批量报到成功结果
     *  @param {Object} data
     * **/
    batchSignResult: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "batchSignResult",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
            //registerId, signType, worker
        } catch (e) {
            // AndroidSDK.writeLog(`调用安卓 ReportMachineAction-> batchSignResult 失败 -- > ` + JSON.stringify(e?.stack));
        }
    },
    /***
     *  @desc 单个报到成功结果
     *  @param {Object} data
     * **/
    singleSignResult: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "singleSignResult",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
            //registerId, signType, worker
        } catch (e) {
            // AndroidSDK.writeLog(`调用安卓 ReportMachineAction-> singleSignResult 失败 -- > ` + JSON.stringify(e?.stack));
        }
    },
    /**
     * 调用打印小票
     * @param data
     */
    takeNumberTicket: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "takeNumberTicket",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog(`调用安卓 ReportMachineAction-> takeNumberTicket 失败 -- > ` + JSON.stringify(e?.stack));
        }
    },

    /**
     * 调用打印小票 --没有报到成功提示
     * @param data
     */
    reprintTicket: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "reprintTicket",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog(`调用安卓 ReportMachineAction-> takeNumberTicket 失败 -- > ` + JSON.stringify(e?.stack));
        }
    },

    /***
     *  @desc 批量报到成功结果
     *  @param {Object} data
     * **/
    reportBatchResult: (data) => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: "",
                bizFun: "batchSignResult",
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
            //registerId, signType, worker
        } catch (e) {
            // AndroidSDK.writeLog("reportBatchResult -->" + JSON.stringify(e?.stack));
        }
    },


    /**
     *  @desc 报到机通用方法
     *  @param {Object} data 传递数据
     *  @param {string} biz 调用安卓的方法
     *  @param {string} callback 回调方法，安卓放在bizType 传递给前端
     * **/
    reportSDKServices: (biz, data = {}, callback = "") => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: callback,
                bizFun: biz,
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog("reportSDKServices --> " + JSON.stringify(e?.stack));
        }
    },

    reportSDKService: (biz, data = "", callback = "") => {
        try {
            let str = JSON.stringify({
                action: "ReportMachineAction",
                callback: callback,
                bizFun: biz,
                data: data
            });
            sendHandleMessage(str);
        } catch (e) {
            AndroidSDK.writeLog("reportSDKService --> " + JSON.stringify(e?.stack));
        }
    },

    queueSwitchAction: (data = {}, callback = "") => {
        try {
            let str = JSON.stringify({
                action: "QueueSwitchAction",
                callback: callback,
                data: JSON.stringify(data)
            });
            sendHandleMessage(str);
        } catch (e) {
            // AndroidSDK.writeLog("queueSwitchAction -->" + JSON.stringify(e?.stack));
        }
    },

};

export {AndroidSDK};

